
package com.bluelinelabs.conductor.demo.controllers;

import com.bluelinelabs.conductor.conductor.changehandler.HorizontalChangeHandler;
import com.bluelinelabs.conductor.conductor.manager.ObjectManager;
import com.bluelinelabs.conductor.demo.ResourceTable;
import com.bluelinelabs.conductor.demo.controllers.base.BaseController;
import com.bluelinelabs.conductor.demo.slice.MainAbilitySlice;
import ohos.aafwk.content.IntentParams;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import static com.bluelinelabs.conductor.conductor.RouterTransaction.with;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class MasterDetailListController extends BaseController {
    public static final String KEY_SELECTED_INDEX = "MasterDetailListController.selectedIndex";
    private DetailItemAdapter adapter;

    public MasterDetailListController() {
        super(ResourceTable.Layout_controller_master_detail_list);
    }


    private int selectedIndex = 0;
    private boolean twoPaneView = false;

    private DetailItemModel[] dataModels = {
            new DetailItemModel(
                    "Item 1",
                    "This is a quick demo of master/detail flow using Conductor. In portrait mode you'll see a standard list. In landscape, you'll see a two-pane layout.",
                    ResourceTable.Color_green_300
            ),
            new DetailItemModel(
                    "Item 2",
                    "This is another item.",
                    ResourceTable.Color_cyan_300
            ),
            new DetailItemModel(
                    "Item 3",
                    "Wow, a 3rd item!",
                    ResourceTable.Color_deep_purple_300
            )
    };

    @Override
    protected void onRestoreInstanceState(IntentParams savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        selectedIndex = (int) savedInstanceState.getParam(KEY_SELECTED_INDEX);
    }


    @Override
    public void onViewCreated(Component component) {
        title = "Master/Detail Flow";

        ObjectManager.getInstance().setIdTitleAboutSetVisibility(Component.HIDE);
        ListContainer Id_recycler_view = (ListContainer) component.findComponentById(ResourceTable.Id_recycler_view);
        adapter = new DetailItemAdapter(dataModels);
        component.getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
            @Override
            public void run() {
                Id_recycler_view.setItemProvider(adapter);
            }
        }, 600);
        if (twoPaneView) {
            onRowSelected(selectedIndex);
        }

        int displayOrientation = ((MainAbilitySlice) getActivity()).getDisplayOrientation();
        switch (displayOrientation) {
            case 0:
            case 2:
                Id_recycler_view.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
                break;
            case 1:
            case 3:
                Id_recycler_view.setWidth(400);
                twoPaneView = true;
                onRowSelected(selectedIndex);
                break;
        }
    }


    public void onRowSelected(int index) {
        dataModels[selectedIndex] = dataModels[selectedIndex].copy(false);
        selectedIndex = index;
        dataModels[selectedIndex] = dataModels[selectedIndex].copy(true);

        DetailItemModel model = dataModels[selectedIndex];
        IntentParams intentParams = new IntentParams();
        intentParams.setParam(ChildController.KEY_TITLE, model.detail);
        intentParams.setParam(ChildController.KEY_BG_COLOR, getView().getContext().getString(model.backgroundColor));
        intentParams.setParam(ChildController.KEY_COLOR_IS_RES, true);

        ChildController controller = new ChildController(intentParams);
        if (twoPaneView) {
//            getChildRouter(binding.detailContainer!!).setRoot(with(controller))
            ((Text) getView().findComponentById(ResourceTable.Id_detail_Text)).setVisibility(Component.HIDE);
            ((DirectionalLayout) getView().findComponentById(ResourceTable.Id_detail_Parent)).setOrientation(DirectionalLayout.HORIZONTAL);
            StackLayout Id_detail_container = (StackLayout) getView().findComponentById(ResourceTable.Id_detail_container);
            Id_detail_container.setVisibility(Component.VISIBLE);
            getChildRouter(Id_detail_container).setRoot(with(controller));
        } else {
            getRouter().pushController(
                    with(controller)
                            .pushChangeHandler(new HorizontalChangeHandler())
                            .popChangeHandler(new HorizontalChangeHandler())
            );
        }
    }

    private static final class DetailItemModel {
        String title;
        String detail;
        int backgroundColor;
        boolean selected = false;

        public DetailItemModel(String title, String detail, int backgroundColor) {
            this.title = title;
            this.detail = detail;
            this.backgroundColor = backgroundColor;
        }

        public DetailItemModel copy(boolean selected) {
            this.selected = selected;
            return this;
        }

    }

    public class DetailItemAdapter extends BaseItemProvider {

        private DetailItemModel[] items;

        public DetailItemAdapter(DetailItemModel[] items) {
            this.items = items.clone();
        }


        @Override
        public int getCount() {
            return items.length;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {

            Component cpt;
            if (convertComponent == null) {
                cpt = LayoutScatter.getInstance(componentContainer.getContext()).parse(ResourceTable.Layout_row_detail_item, componentContainer, false);
            } else {
                cpt = convertComponent;
            }

            DetailItemModel dataModel = dataModels[position];
            Text Id_title = (Text) cpt.findComponentById(ResourceTable.Id_title);
            Id_title.setText(dataModel.title);


            if (dataModel.selected) {
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor(cpt.getContext().getString(ResourceTable.Color_grey_400))));
                cpt.setBackground(shapeElement);
            } else {
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#00000000")));
                cpt.setBackground(shapeElement);
            }

            cpt.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    onRowSelected(position);
                    notifyDataChanged();
                }
            });

            return cpt;
        }
    }
}
